// pages/module/visit/visit.js
import regeneratorRuntime from '../../../utils/runtime.js';
import img_list from '../../../utils/util.js';
import { getData, setData, clearData, dataKey } from '../../../utils/storage.js';
import shareFun from '../../../utils/shareFun.js';
import {
  qrcode,
  visiteImg,
  visitList,
  recordGrowth,
  promotion_img,
} from '../../../utils/imapi.js';
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    show:0,
    cardCur:0,
	images:'',
	user:'',
	tt:true,
    swiperList:[
      {
        id: 0,
        type: 'image',
        url: '/images/index/share_test.png'
      },
      {
        id: 0,
        type: 'image',
        url: '/images/index/share_test.png'
      },
      {
        id: 0,
        type: 'image',
        url: '/images/index/share_test.png'
      },
      {
        id: 0,
        type: 'image',
        url: '/images/index/share_test.png'
      },
      {
        id: 0,
        type: 'image',
        url: '/images/index/share_test.png'
      },
    ],
    img_list:"img_list.img_list"
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad:async function (options) {
    this.setData({
      img_list:img_list.img_list
    })
    var that = this
    shareFun.hidenShareMenu()
    
    var visitListData = ''
    if(!app.globalData.visitListData){

      wx.showLoading({
        title: '加载中',
      })
      
      visitListData = await visitList({opt:1})
      app.globalData.visitListData = visitListData

      wx.hideLoading()

    }else{
      visitListData = app.globalData.visitListData
    }

    console.log(" === visitListData:"+app.globalData.visitListData)

    this.setData({
      swiperList:visitListData.data
    })
  
    let scene = '10006,'+app.globalData.userInfo.uid
    var qrcodeData = await qrcode({scene:scene})

 //    var img=ArrayBuffer(qrcodeData.data);
	// var im=decode(img);
 //    console.log(im)
	this.setData({
	  images:qrcodeData,
	  user:app.globalData.userInfo
	})
	
  },
  calculate(value){
    var _value = this.screenWidth / 750 * value
    return _value
  },
  imgCombine(url){
    var that = this;
    wx.getImageInfo({
      src: url,//
      success (res) {
        that.ctx.save()
        that.ctx.drawImage(res.path, 0, 0, that.screenWidth, that.calculate(1624))
        that.ctx.setTextAlign('left')
        that.ctx.setFillStyle('#FFFFFF')
        that.ctx.setFontSize(that.calculate(32))
        that.ctx.fillText(that.user_info.name, that.calculate(332), that.calculate(1412));
        wx.getImageInfo({
          src: that.user_info.headimgurl,//
          success (res0) {
            that.ctx.arc(that.calculate(290), that.calculate(1400), that.calculate(24), 0, 2*Math.PI)
            that.ctx.clip()
            that.ctx.drawImage(res0.path, that.calculate(0), that.calculate(0),res0.width,res0.height,that.calculate(266), that.calculate(1376),that.calculate(48), that.calculate(48))
            that.ctx.restore()
            that.ctx.arc(that.calculate(164), that.calculate(1394), that.calculate(84), 0, 2*Math.PI)
            that.ctx.clip()
            that.ctx.drawImage(that.codeImg, that.calculate(0), that.calculate(0),280,280,that.calculate(80), that.calculate(1310),that.calculate(168), that.calculate(168))
            that.ctx.draw(false,()=>{
              wx.canvasToTempFilePath({
                canvasId: 'canvas',
                success: (res2) => {
                  wx.hideLoading({})
                  that.setData({
                    swiperList:[res2.tempFilePath]
                  })
                }
              },that)
            })
          }
        })
      },
      fail(res){
        console.log(res)
      }
    })
  },
  imgLoad(){
    this.setData({
      show:1
    })
  },
  cardSwiper(e) {
    this.setData({
      cardCur: e.detail.current
    })
  },
  downloadImg:async function(e){
         
    // wx.downloadFile({
    //   url: this.data.swiperList[this.data.cardCur],　　　　　　　//需要下载的图片url
    //   success: function (res) {　　　　　　　　　　　　//成功后的回调函数

    if (this.data.swiperList[this.data.cardCur].indexOf("data")==-1){

        wx.showLoading({
          title: '推广海报合成中',
        })

      // let idx=e.currentTarget.dataset.index
      // 后端显示推广码 update 20200810
      const link_data = await promotion_img({
        bg: this.data.swiperList[this.data.cardCur],
        opt:1,
        uid:app.globalData.userInfo.uid,
        path:'pages/tabbar/index/index',
        //scene:'share_uid='+app.globalData.userInfo.uid+'&scene=10006' ,
       // scene:'share_uid='+app.globalData.userInfo.uid, //场景值超过长度
        scene:'10006,'+ app.globalData.userInfo.uid // 场景,场景值 => 10006,10001  ; 场景 10006, 10001 用户 uid
      })
      wx.hideLoading()

      this.data.swiperList[this.data.cardCur]=link_data.data
      this.setData({
        swiperList:this.data.swiperList
      })
      /*

      this.setData({
        swiperList[idx]:link_data,
      })*/
    }
            
    var aa = wx.getFileSystemManager();
    let t =new Date().getTime()
    aa.writeFile({
      filePath:wx.env.USER_DATA_PATH+'/'+ t+'.png',
      data: this.data.swiperList[this.data.cardCur].slice(22),
      encoding:'base64',
      success: res => {
        wx.saveImageToPhotosAlbum({
          filePath: wx.env.USER_DATA_PATH +'/' +t+'.png',
          success: function (res) {
            wx.showToast({
              title: '保存成功',
            })
			
          },
          fail: function (err) {
            console.log(err)
          }
        })
        console.log(res)
      }, fail: err => {
        console.log(err)
      }
    })
     
    /*

        wx.saveImageToPhotosAlbum({　　　　　　　　　//保存到本地
          filePath: this.data.swiperList[this.data.cardCur],
          success(res) {
            wx.showToast({
              title: '保存成功',
              icon: 'success',
              duration: 2000
            })
          },
          fail: function (err) {
            if (err.errMsg === "saveImageToPhotosAlbum:fail auth deny") {
              wx.openSetting({
                success(settingdata) {
                  console.log(settingdata)
                  if (settingdata.authSetting['scope.writePhotosAlbum']) {
                    console.log('获取权限成功，给出再次点击图片保存到相册的提示。')
                  } else {
                    console.log('获取权限失败，给出不给权限就无法正常使用的提示')
                  }
                }
              })
            }
          }
        })*/
    //   }
    // });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  bthed:async function () {
    
    if(this.data.swiperList[this.data.cardCur].indexOf("data")==-1){
      wx.showLoading({
          title: '海报合成中',
        })
      
      
      const link_data = await promotion_img({
        bg: this.data.swiperList[this.data.cardCur],
        opt:1,
        uid:app.globalData.userInfo.uid,
        path:'pages/tabbar/index/index',
        //scene:'share_uid='+app.globalData.userInfo.uid+'&scene=10006' ,
      // scene:encodeURIComponent('share_uid='+app.globalData.userInfo.uid), //场景值超过长度
        scene: '10006,'+app.globalData.userInfo.uid //场景，用户uid
      })
      wx.hideLoading()
      
      const current = link_data.data  //获取当前点击的 图片 url
      var url=[];
      url.push(current);
      wx.previewImage({
          current:current,
          urls: url
      })
      return;
    }
    
    var url=[];
    url.push(this.data.swiperList[this.data.cardCur]);
    wx.previewImage({
        current:this.data.swiperList[this.data.cardCur],
        urls: url
    })
    
  }
})